package com.yanglei.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ClientSendFileThread extends Thread {
	private ClientConnection clientConnetction;

	private FileInputStream fileInputStream;

	private FileOutputStream fileOutStream;

	private DataInputStream dateInputStream;

	private DataOutputStream dataOutStream;
	
	private ServerSocket serverSocket;
	
	private Socket socket;
	
	private int port;
	
	private String address;

	public ClientSendFileThread(ClientConnection clientConnetction,int port,String address) {
		super();
		this.clientConnetction = clientConnetction;
		this.fileOutStream = this.clientConnetction.getFileOutStream();
		this.fileInputStream = this.clientConnetction.getFileInputStream();
		this.dateInputStream = this.clientConnetction.getDateInputStream();
		this.dataOutStream = this.clientConnetction.getDataOutStream();
		this.port=port;
		this.address=address;
	
	}
	@Override
	public void run() {
		try {
			System.out.println("send");
			System.out.println(address);
			System.out.println(port);
			this.socket=new Socket(address,this.port);
			this.fileInputStream = new FileInputStream(this.clientConnetction
					.getSingleClient().getFilePath());

			this.dataOutStream=new DataOutputStream(this.socket.getOutputStream());
			
			byte[] b = new byte[1];
			while (this.fileInputStream.read(b) != -1) {
				this.dataOutStream.write(b);
			}
			
			
		} catch (Exception e) {
			// TODO: handle exception
		}finally
		{
			try {
				this.serverSocket.close();
				this.socket.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
	}
	
 
}
